home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / dialogs.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  12.6 KB  |  327 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Handle dialog popups.
  5.  
  6. Simple Choices:
  7.     For dialogs where you just want to ask the user a question use the
  8.     ChoiceDialog class.  Pass it a title, description and a the buttons to
  9.     display.  The call dialog.run, passing it a callback.  Here\'s an example:
  10.  
  11.     dialog = dialog.ChoiceDialog("Do you like pizza?",
  12.         "Democracy would like to know if you enjoy eating pizza.",
  13.         dialog.BUTTON_YES, dialog.BUTTON_NO)
  14.     def handlePizzaAnswer(dialog):
  15.         if dialog.choice is None:
  16.             # handle the user closing the dialog windows
  17.         elif dialog.choice == dialog.BUTTON_YES:
  18.            # handle yes response
  19.         elif dialog.choice == dialag.BUTTON_NO:
  20.             # handle no respnose
  21.     dialog.run(handlePizzaAnswer)
  22.  
  23. Advanced usage:
  24.     For more advanced usage, check out the other Dialog subclasses.  They will
  25.     probably have different constructor arguments and may have attributes
  26.     other than choice that will be set.  For example, the HTTPAuthDialog has a
  27.     "username" and "password" attribute that store what the user entered in
  28.     the textboxes.
  29.  
  30. Frontend requirements:
  31.     Frontends should implement the runDialog method in UIBackendDelegate
  32.     class.  It inputs a Dialog subclass and displays it to the user.  When the
  33.     user clicks on a button, or closes the dialog window, the frontend must
  34.     call dialog.runCallback().
  35.  
  36.     As we add new dialog boxes, the frontend may run into Dialog subclasses
  37.     that it doesn\'t recognize.  In that case, call dialog.runCallback(None).
  38.  
  39.     The frontend can layout the window however it wants, in particular buttons
  40.     can be arranged with the default on the right or the left depending on the
  41.     platform (The default button is the 1st button in the list).  Frontends
  42.     should try to recognize standard buttons and display the stock icons for
  43.     them.  
  44.     '''
  45. import eventloop
  46. from gtcache import gettext as _
  47.  
  48. def setDelegate(newDelegate):
  49.     global delegate
  50.     delegate = newDelegate
  51.  
  52.  
  53. class DialogButton(object):
  54.     
  55.     def __init__(self, text):
  56.         self.text = text
  57.  
  58.     
  59.     def __eq__(self, other):
  60.         if isinstance(other, DialogButton):
  61.             pass
  62.         return self.text == other.text
  63.  
  64.     
  65.     def __str__(self):
  66.         return 'DialogButton(%r)' % self.text
  67.  
  68.  
  69. BUTTON_OK = DialogButton(_('Ok'))
  70. BUTTON_CANCEL = DialogButton(_('Cancel'))
  71. BUTTON_YES = DialogButton(_('Yes'))
  72. BUTTON_NO = DialogButton(_('No'))
  73. BUTTON_QUIT = DialogButton(_('Quit'))
  74. BUTTON_IGNORE = DialogButton(_('Ignore'))
  75. BUTTON_SUBMIT_REPORT = DialogButton(_('Submit Crash Report'))
  76. BUTTON_MIGRATE = DialogButton(_('Migrate'))
  77. BUTTON_DONT_MIGRATE = DialogButton(_("Don't Migrate"))
  78. BUTTON_DOWNLOAD = DialogButton(_('Download'))
  79. BUTTON_REMOVE_ENTRY = DialogButton(_('Remove Entry'))
  80. BUTTON_DELETE_FILE = DialogButton(_('Delete File'))
  81. BUTTON_DELETE_FILES = DialogButton(_('Delete Files'))
  82. BUTTON_KEEP_VIDEOS = DialogButton(_('Keep Videos'))
  83. BUTTON_DELETE_VIDEOS = DialogButton(_('Delete Videos'))
  84. BUTTON_CREATE = DialogButton(_('Create'))
  85. BUTTON_CREATE_CHANNEL = DialogButton(_('Create Channel'))
  86. BUTTON_ADD = DialogButton(_('Add'))
  87. BUTTON_ADD_INTO_NEW_FOLDER = DialogButton(_('Add Into New Folder'))
  88. BUTTON_KEEP = DialogButton(_('Keep'))
  89. BUTTON_DELETE = DialogButton(_('Delete'))
  90. BUTTON_NOT_NOW = DialogButton(_('Not Now'))
  91. BUTTON_CLOSE_TO_TRAY = DialogButton(_('Close to Tray'))
  92. BUTTON_LAUNCH_MIRO = DialogButton(_('Launch Miro'))
  93. BUTTON_DOWNLOAD_ANYWAY = DialogButton(_('Download Anyway'))
  94.  
  95. class Dialog(object):
  96.     '''Abstract base class for dialogs.'''
  97.     
  98.     def __init__(self, title, description, buttons):
  99.         self.title = title
  100.         self.description = description
  101.         self.buttons = buttons
  102.  
  103.     
  104.     def run(self, callback):
  105.         self.callback = callback
  106.         self.choice = None
  107.         delegate.runDialog(self)
  108.  
  109.     
  110.     def runCallback(self, choice):
  111.         '''Run the callback for this dialog.  Choice should be the button that
  112.         the user clicked, or None if the user closed the window without
  113.         makeing a selection.
  114.         '''
  115.         self.choice = choice
  116.         eventloop.addUrgentCall(self.callback, '%s callback' % self.__class__, args = (self,))
  117.  
  118.  
  119.  
  120. class MessageBoxDialog(Dialog):
  121.     '''Show the user some info in a dialog box.  The only button is Okay.  The
  122.     callback is optional for a message box dialog.  '''
  123.     
  124.     def __init__(self, title, description):
  125.         Dialog.__init__(self, title, description, [
  126.             BUTTON_OK])
  127.  
  128.     
  129.     def run(self, callback = None):
  130.         Dialog.run(self, callback)
  131.  
  132.     
  133.     def runCallback(self, choice):
  134.         if self.callback is not None:
  135.             Dialog.runCallback(self, choice)
  136.         
  137.  
  138.  
  139.  
  140. class ChoiceDialog(Dialog):
  141.     """Give the user a choice of 2 options (Yes/No, Ok/Cancel,
  142.     Migrate/Don't Migrate, etc.)
  143.     """
  144.     
  145.     def __init__(self, title, description, defaultButton, otherButton):
  146.         super(ChoiceDialog, self).__init__(title, description, [
  147.             defaultButton,
  148.             otherButton])
  149.  
  150.  
  151.  
  152. class ThreeChoiceDialog(Dialog):
  153.     '''Give the user a choice of 3 options (e.g. Remove entry/
  154.     Delete file/Cancel).
  155.     '''
  156.     
  157.     def __init__(self, title, description, defaultButton, secondButton, thirdButton):
  158.         super(ThreeChoiceDialog, self).__init__(title, description, [
  159.             defaultButton,
  160.             secondButton,
  161.             thirdButton])
  162.  
  163.  
  164.  
  165. class HTTPAuthDialog(Dialog):
  166.     '''Ask for a username and password for HTTP authorization.  Frontends
  167.     should create a dialog with text entries for a username and password.  Use
  168.     prefillUser and prefillPassword for the initial values of the entries.
  169.  
  170.     The buttons are always BUTTON_OK and BUTTON_CANCEL.
  171.     '''
  172.     
  173.     def __init__(self, url, realm, prefillUser = None, prefillPassword = None):
  174.         desc = 'location %s requires a username and password for "%s".' % (url, realm)
  175.         super(HTTPAuthDialog, self).__init__('Login Required', desc, (BUTTON_OK, BUTTON_CANCEL))
  176.         self.prefillUser = prefillUser
  177.         self.prefillPassword = prefillPassword
  178.  
  179.     
  180.     def runCallback(self, choice, username = '', password = ''):
  181.         self.username = username
  182.         self.password = password
  183.         super(HTTPAuthDialog, self).runCallback(choice)
  184.  
  185.  
  186.  
  187. class SearchChannelDialog(Dialog):
  188.     '''Ask for information to create a new search channel.  Frontends
  189.     should create a dialog with all sorts of fields.  Use the given
  190.     values for the initial values and replace the values if the user
  191.     selects anything.
  192.  
  193.     The buttons are always BUTTON_CREATE_CHANNEL and BUTTON_CANCEL.
  194.     '''
  195.     CHANNEL = 0
  196.     ENGINE = 1
  197.     URL = 2
  198.     
  199.     def __init__(self, term = None, style = CHANNEL, location = None):
  200.         import views
  201.         import indexes
  202.         import util
  203.         RSSFeedImpl = RSSFeedImpl
  204.         ScraperFeedImpl = ScraperFeedImpl
  205.         import feed
  206.         self.term = term
  207.         self.style = style
  208.         self.location = location
  209.         self.channels = []
  210.         
  211.         def shorten(s):
  212.             if len(s) > 50:
  213.                 return s[:50] + u'...'
  214.             
  215.             return s
  216.  
  217.         for feed in views.feeds:
  218.             if feed.actualFeed.__class__ in (RSSFeedImpl, ScraperFeedImpl):
  219.                 self.channels.append((feed.id, shorten(feed.getTitle())))
  220.                 continue
  221.         
  222.         self.engines = []
  223.         for engine in views.searchEngines:
  224.             self.engines.append((engine.name, engine.title))
  225.         
  226.         if style == self.CHANNEL and self.location == -1:
  227.             self.location = self.channels[2][0]
  228.         
  229.         searchFeed = util.getSingletonDDBObject(views.feeds.filterWithIndex(indexes.feedsByURL, 'dtv:search'))
  230.         self.defaultEngine = searchFeed.lastEngine
  231.         super(SearchChannelDialog, self).__init__(_('New Search Channel'), _('A search channel contains items that match a search term.'), (BUTTON_CREATE_CHANNEL, BUTTON_CANCEL))
  232.  
  233.     
  234.     def getURL(self):
  235.         urlencode = urlencode
  236.         import xhtmltools
  237.         import searchengines
  238.         defaultDatabase = defaultDatabase
  239.         import database
  240.         term = self.term
  241.         location = self.location
  242.         style = self.style
  243.         if not term or not location:
  244.             return None
  245.         
  246.         if style == self.CHANNEL:
  247.             channel = defaultDatabase.getObjectByID(location)
  248.             if channel:
  249.                 style = self.URL
  250.                 location = channel.getBaseURL()
  251.                 searchTerm = channel.getSearchTerm()
  252.                 if searchTerm is not None:
  253.                     term = searchTerm + ' ' + term
  254.                 
  255.             
  256.         
  257.         if type(term) == unicode:
  258.             term = term.encode('utf8')
  259.         
  260.         if type(location) == unicode:
  261.             location = location.encode('utf8')
  262.         
  263.         if style == self.ENGINE:
  264.             return searchengines.getRequestURL(location, term)
  265.         
  266.         if style == self.URL:
  267.             return 'dtv:searchTerm:%s?%s' % (urlencode(location), urlencode(term))
  268.         
  269.  
  270.  
  271.  
  272. class TextEntryDialog(Dialog):
  273.     '''Like the ChoiceDialog, but also contains a textbox for the user to
  274.     enter a value into.  This is used for things like the create playlist
  275.     dialog, the rename dialog, etc.
  276.     '''
  277.     
  278.     def __init__(self, title, description, defaultButton, otherButton, prefillCallback = None, fillWithClipboardURL = False):
  279.         super(TextEntryDialog, self).__init__(title, description, [
  280.             defaultButton,
  281.             otherButton])
  282.         self.prefillCallback = prefillCallback
  283.         self.fillWithClipboardURL = fillWithClipboardURL
  284.  
  285.     
  286.     def runCallback(self, choice, value = None):
  287.         self.value = value
  288.         super(TextEntryDialog, self).runCallback(choice)
  289.  
  290.  
  291.  
  292. class CheckboxDialog(Dialog):
  293.     """Like the ChoiceDialog, but also contains a checkbox for the user to
  294.     enter a value into.  This is used for things like asking whether to show
  295.     the dialog again.  There's also a mesage for the checkbox and an initial
  296.     value.
  297.     """
  298.     
  299.     def __init__(self, title, description, checkbox_text, checkbox_value, defaultButton, otherButton):
  300.         super(CheckboxDialog, self).__init__(title, description, [
  301.             defaultButton,
  302.             otherButton])
  303.         self.checkbox_text = checkbox_text
  304.         self.checkbox_value = checkbox_value
  305.  
  306.     
  307.     def runCallback(self, choice, checkbox_value = False):
  308.         self.checkbox_value = checkbox_value
  309.         super(CheckboxDialog, self).runCallback(choice)
  310.  
  311.  
  312.  
  313. class CheckboxTextboxDialog(CheckboxDialog):
  314.     '''Like CheckboxDialog but also with a text area. Used for
  315.     capturing bug report data'''
  316.     
  317.     def __init__(self, title, description, checkbox_text, checkbox_value, textbox_value, defaultButton, otherButton):
  318.         super(CheckboxTextboxDialog, self).__init__(title, description, checkbox_text, checkbox_value, defaultButton, otherButton)
  319.         self.textbox_value = textbox_value
  320.  
  321.     
  322.     def runCallback(self, choice, checkbox_value = False, textbox_value = ''):
  323.         self.textbox_value = textbox_value
  324.         super(CheckboxTextboxDialog, self).runCallback(choice, checkbox_value)
  325.  
  326.  
  327.